<?php
class Productcategory_model extends CI_Model {

    function __construct()
    {
        parent::__construct();
    }

    /*
    查询是否有子类
    */
    function ExecuteHasSubclass($parentid)
    {
        //$this->load->database();
        $sql = "SELECT COUNT(0) FROM productcategory WHERE ParentId=".$parentid; 
        //print_r($sql);
        $this->load->library('mysqlidb');
        $result = $this->mysqlidb->data_scalar($sql);
        //print_r($result);
        return $result;

    }
    /*
	查询子类
    */
    function GetListByParent($parentid)
    {
    	$this->load->database();
    	$sql = "SELECT * FROM productcategory WHERE ParentId=".$parentid; 

		$query = $this->db->query($sql);

		if ($query->num_rows() > 0)
		{
		   return $query->result_array(); 		 
		}
		
    }

    /**
    *  分页查询
    *  $pageindex页码,$pagesize页大小，$recordcount返回的总记录数
    */
    function GetListByPagination($pageindex,$pagesize,&$recordcount)
    {
        if ($pageindex<1) $pageindex = 1; 
        $pagestart = ($pageindex - 1) * $pagesize;
        $this->load->library('mysqlidb');

        $sql = "SELECT SQL_CALC_FOUND_ROWS  * FROM productcategory LIMIT ".$pagestart.",".$pagesize.";SELECT FOUND_ROWS() ;"; 

        $result = $this->mysqlidb->querypagination($sql);
        $recordcount = $result[1][0][0];
        //print_r($sql);
        return $result[0];
    }



 /**
    * 获取分类树结构
    */
    function GetCategoryTree($parentid = 0)
    {
        $list = array();
        $templist = $this->GetCategoryListByParent($parentid);
        $qz = "├";//前缀
        foreach ($templist as $item) {
            $listindex = count($list);
            $item['Sort']= $listindex;
            $list[$listindex] = $item;
            $this->GetCategoryListByParentId($item['CategoryId'],$qz,$list);
        }
        return $list;
        //print_r($list);
    }

    /**
    * 获取分类树结构
    */
    function GetCategoryListByParentId($parentId,$qz,&$list)
    {
        $temp = $this->GetCategoryListByParent($parentId);
        if(count($temp)>0) {
            $qz = $qz."─ ";
        }
        if(is_array($temp))    
        {                            
            foreach ($temp as $item) {
                $listindex = count($list);
                $item['Sort']= ($listindex + 1);
                $item['CategoryName'] = $qz . $item['CategoryName'];
                $list[$listindex] = $item;
                $this->GetCategoryListByParentId($item['CategoryId'],$qz,$list);
            }
        }
    }

    /*
    查询指定父类下面的子类
    $parentId 父类ID（int）
    */
    function GetCategoryListByParent($parentId)
    {
        $this->load->database();
        $sql = "SELECT * FROM productcategory WHERE ParentId = ?;"; 


        $query = $this->db->query($sql, array($parentId));

        if ($query->num_rows() > 0)
        {
           return $query->result_array();         
        }
    }
    



    /**
    * 获取实体
    */
    function GetEntity($id)
    {
        $this->load->database();
        $sql = "SELECT * FROM productcategory WHERE CategoryId = ? LIMIT 1;"; 


        $query = $this->db->query($sql, array($id));

        if ($query->num_rows() > 0)
        {
           return $query->row();         
        }
    }

    /**
    * 添加分类
    */
    function Insert($CategoryName,$Comment,$parentid)
    {
        $data = array('CategoryName' => $CategoryName,'Comment' => $Comment,'ParentId' => $parentid,'CreateOn' => 'NOW()','Status' => 1,'CreateBy' => 'system','ModifyOn' => 'NOW()' );
        //$sql = "INSERT INTO newscategory (CategoryName,ParentId,Comment,CreateOn,CreateBy,ModifyOn) VALUES (?,?,?,NOW(),?,NOW());";
        $this->load->database();
        $result = $this->db->insert('productcategory',$data);
        return $result;
    }

    /**
    * 修改分类
    */
    function Update($id,$CategoryName,$Comment,$parentid)
    {
        $data = array('CategoryName' => $CategoryName,'Comment' => $Comment,'ParentId' => $parentid,'ModifyBy' => 'system','ModifyOn' => 'NOW()' );
        $where = array('CategoryId' => $id );

        $this->load->database();
        $result = $this->db->update('productcategory',$data,$where);
        return $result;
    }

    /**
    * 删除
    */
    function Delete($id)
    {
        $where = array('CategoryId' => $id );
        $this->load->database();
        $result = $this->db->delete('productcategory',$where);
        return $result;
    }
}

?>